package cn.kgc.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断session是否为空
        HttpSession session=request.getSession();
        Object user=session.getAttribute("user");
        System.out.println(user);

        if(user==null){
            System.out.println("拦截请求："+request.getRequestURI());
            //如果没有登录，则去到登录页面
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return false;//验证失败，进行拦截
        }
        //验证通过
        System.out.println("-->放行请求："+request.getRequestURI());
        return true;
    }
}
